﻿#region << 版 本 注 释 >>
/*
 * ========================================================================
 * Copyright(c) 2008-2011深圳市宇商网科技有限公司, All Rights Reserved.
 * ========================================================================
 *  
 * 【当前类文件的功能】
 *  
 *  
 * 作者：[chao.jiang]   时间：2012-6-12 11:01:41
 * 文件名：AppConfig
 * 版本：V1.0.0
 * 
 * 修改者：           时间：               
 * 修改说明：
 * ========================================================================
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.IO;
using System.Configuration;

namespace EWineShop.Config
{
    public class AppConfig
    {
        #region 私有
        private XmlDocument doc;
        private string _fileName;
        private string _filePath;
        private bool _ingoreNullNode = false;
        #endregion

        #region Properties
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }

        /// <summary>
        /// 是否忽略空节点
        /// </summary>
        public bool IngoreNullNode
        {
            get { return _ingoreNullNode; }
            set { _ingoreNullNode = value; }
        }
        #endregion

        #region AppConfig
        /// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="fileName">文件名</param>
        public AppConfig(string fileName)
        {
            this._fileName = fileName;
        }

        /// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="ingoreNullNode">是否忽略空节点</param>
        public AppConfig(string fileName, bool ingoreNullNode)
        {
            this._fileName = fileName;
            this._ingoreNullNode = ingoreNullNode;
        }
        #endregion

        #region 增删改查

        #region Load
        /// <summary>
        /// 载入配置文件
        /// </summary>
        public void Load()
        {
            if (this._fileName != null)
            {
                this._filePath = HttpContext.Current.Server.MapPath(this._fileName);
            }
            else if (this._filePath != null)
            {
                this._fileName = Path.GetFileName(this._filePath);
            }
            else
            {
                throw new ArgumentException("未指定配置文件");
            }

            doc = new XmlDocument();

            try
            {
                doc.Load(this._filePath);
            }
            catch
            {
                throw new ArgumentException(string.Format("配置文件 \"{0}\" 不存在", _fileName));
            }
        }
        #endregion

        #region Save
        /// <summary>
        /// 保存配置文件
        /// </summary>
        public void Save()
        {
            try
            {
                doc.Save(_filePath);
            }
            catch (Exception e)
            {

                throw new ArgumentException(string.Format("配置文件 \"{0}\" 保存失败", this._fileName) + e.Message);
            }
        }
        #endregion

        #region GetNode
        private XmlNode GetNode(string key)
        {
            string xPath = string.Format("/Settings/Add[@Key='{0}']", key);
            XmlNode node = doc.SelectSingleNode(xPath);
            return node;
        }
        #endregion

        #region Select
        /// <summary>
        /// 从配置文件中查询对应键的值
        /// </summary>
        /// <param name="key">关键字</param>
        /// <returns>查询到的值</returns>
        public string Select(string key)
        {
            XmlNode node = this.GetNode(key);

            if (node == null)
            {
                if (this._ingoreNullNode)
                {
                    return "";
                }
                else
                {
                    throw new ArgumentException(string.Format("配置节点 \"{0}\" 不存在", key));
                }
            }

            return node.Attributes["Value"].Value;
        }
        #endregion

        #region Insert
        /// <summary>
        /// 插入配置内容
        /// </summary>
        /// <param name="key">插入关键字</param>
        /// <param name="value">插入值</param>
        public void Insert(string key, string value)
        {
            XmlNode node = this.GetNode(key);

            if (node != null)
            {
                if (this._ingoreNullNode)
                {
                    this.Update(key, value);
                    return;
                }
                else
                {
                    throw new ArgumentException(string.Format("配置节点 \"{0}\" 已存在", key));
                }
            }

            node = doc.CreateElement("Add");
            XmlAttribute attKey = doc.CreateAttribute("Key");
            XmlAttribute attValue = doc.CreateAttribute("Value");
            attKey.Value = key;
            attValue.Value = value;
            node.Attributes.Append(attKey);
            node.Attributes.Append(attValue);
            doc.DocumentElement.AppendChild(node);
        }
        #endregion

        #region Update
        /// <summary>
        /// 更新配置文件对应关键字的值
        /// </summary>
        /// <param name="key">更新关键字</param>
        /// <param name="value">更新的值</param>
        public void Update(string key, string value)
        {
            XmlNode node = this.GetNode(key);

            if (node == null)
            {
                if (this._ingoreNullNode)
                {
                    this.Insert(key, value);
                    return;
                }
                else
                {
                    throw new ArgumentException(string.Format("配置节点 \"{0}\" 不存在", key));
                }
            }

            node.Attributes["Value"].Value = value;
        }

        /// <summary>
        /// 更新配置文件对应关键字的值
        /// </summary>
        /// <param name="key">更新关键字</param>
        /// <param name="value">更新的值</param>
        /// <param name="create">不存在是是否需要创建</param>
        public void Update(string key, string value, bool create)
        {
            XmlNode node = this.GetNode(key);

            if (node == null)
            {
                if (create)
                {
                    this.Insert(key, value);
                }
                else
                {
                    throw new ArgumentException(string.Format("配置节点 \"{0}\" 不存在", key));
                }
            }
            else
            {
                node.Attributes["Value"].Value = value;
            }
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除配置文件对应关键字
        /// </summary>
        /// <param name="key">关键字</param>
        public void Delete(string key)
        {
            XmlNode node = this.GetNode(key);

            if (node != null)
            {
                doc.DocumentElement.RemoveChild(node);
            }
        }
        #endregion

        #endregion

        /// <summary>
        /// 读取配置节点信息
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <returns></returns>
        public static string GetValue(string name)
        {
            string strValue = ConfigurationManager.AppSettings[name].ToString();
            if (String.IsNullOrEmpty(strValue))
            {
                throw new ArgumentNullException(string.Format("AppSetting配置节找不到key为{0}的配置信息", name));
            }
            return strValue;

        }

        /// <summary>
        /// 从AppSetting中获取Value值。
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private static string getValue(string name)
        {
            string kv = System.Configuration.ConfigurationManager.AppSettings[name];
            if (string.IsNullOrEmpty(kv))
            {
                throw new ArgumentNullException(string.Format("AppSetting配置节找不到key为{0}的配置信息", name));
            }
            return kv;
        }


        #region 前台网站配置文件读取类


        public static string MstContact
        {
            get
            {
                return getValue("MstContact");
            }
        }

        public static string StaticsUrl
        {
            get
            {
                return getValue("StaticsUrl");
            }
        }

        public static string InfoPubOfferCatchCnt
        {
            get
            {
                return getValue("InfoPubOfferCatchCnt");
            }
        }


        public static string StaticResourceVersion
        {
            get
            {
                return getValue("StaticResourceVersion");
            }
        }


        public static string MstLoginInfo
        {
            get
            {
                return getValue("MstLoginInfo");
            }
        }

        public static string MstLogin
        {
            get
            {
                return getValue("MstLogin");
            }
        }

        public static string MstLogout
        {
            get
            {
                return getValue("MstLogout");
            }
        }

        public static string MstInbox
        {
            get
            {
                return getValue("MstInbox");
            }
        }


        public static string OfferPubUrl
        {
            get
            {
                return getValue("OfferPubUrl");
            }
        }

        public static string OfferEditUrl
        {
            get
            {
                return getValue("OfferEditUrl");
            }
        }

        public static string ReturnToOfferInfoUrl
        {
            get
            {
                return getValue("ReturnToOfferInfoUrl");
            }
        }


        public static string ListTemplates
        {
            get
            {
                return getValue("ListTemplates");
            }
        }

        public static string server_Offer_UploadImg
        {
            get
            {
                return getValue("server_Offer_UploadImg");
            }
        }

        #endregion


    }


}



